package org.nabc.generate;

import lombok.extern.slf4j.Slf4j;
import org.nabc.configuration.FileConfiguration;
import org.nabc.core.AbstractFileOperation;
import org.nabc.support.PropertyValues;

@Slf4j
public abstract class AbstractRunnableTask extends AbstractFileOperation implements TemplateGenerateTask {
    /**
     * 内部属性
     */
    protected PropertyValues propertyValues;
    /**
     * 领域模型
     */
    protected String domainModel;
    protected StringBuffer fileTemplateData;
    protected String filePath;
    protected String fileSuffix = "java";
    protected String className;
    protected String entityName;
    /**
     * 使用实体类添加领域模型
     */
    protected Boolean useEntityClassAddDomainModel = true;

    protected String type;
    protected String mode;

    public AbstractRunnableTask() {
        super();
    }

    public void setParameters(FileConfiguration fileConfiguration, PropertyValues propertyValues, String domainModel) {
        setFileConfiguration(fileConfiguration);
        this.propertyValues = propertyValues;
        this.domainModel = domainModel;
        this.entityName = conversionHump(propertyValues.getTableName());
        this.className = this.useEntityClassAddDomainModel ? this.entityName + capitalLetter(this.domainModel) : this.entityName;
        this.filePath = String.format("/%s/%s.%s", this.domainModel, this.className, this.fileSuffix);
    }

    @Override
    public void run() {
        try {
            generateFolder("/" + domainModel);
            if (!generateFile(filePath)) {
                throw new Exception("创建失败");
            }
            log.info("开始创建 {} 文件", this.className);
            generate();
            log.info("创建 {} 文件 完成", this.className);
        } catch (Exception e) {
            log.error("创建 {} 文件 失败", this.className);
        }
    }

    public String getModelAndType() {
        return mode + type;
    }

    public abstract void setParameters(FileConfiguration fileConfiguration, PropertyValues propertyValues);

    public abstract void setModelAndType();
}
